<?php

namespace App\Models\Order;

use App\Models\Asset;
use App\Models\AssetStock;
use Hyn\Tenancy\Traits\UsesTenantConnection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * App\Models\Order\OrderItem
 *
 * @property int $id
 * @property string $order_id
 * @property string $name 资产名称
 * @property int $rentalable_id
 * @property string $rentalable_type
 * @property int $quantity 数量
 * @property float $unit_price 单价
 * @property float $amount 价格
 * @property string|null $rental_out_at 出库时间
 * @property string|null $return_at 入库时间
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property \Illuminate\Support\Carbon|null $deleted_at
 * @property-read \App\Models\Order\Order $order
 * @property-read \Illuminate\Database\Eloquent\Model|\Eloquent $rentalable
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order\OrderItem assetStocks()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order\OrderItem assets()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order\OrderItem extraFee()
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order\OrderItem newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order\OrderItem newQuery()
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Order\OrderItem onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order\OrderItem query()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order\OrderItem whereAmount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order\OrderItem whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order\OrderItem whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order\OrderItem whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order\OrderItem whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order\OrderItem whereOrderId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order\OrderItem whereQuantity($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order\OrderItem whereRentalOutAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order\OrderItem whereRentalableId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order\OrderItem whereRentalableType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order\OrderItem whereReturnAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order\OrderItem whereUnitPrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Order\OrderItem whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Order\OrderItem withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Order\OrderItem withoutTrashed()
 * @mixin \Eloquent
 */
class OrderItem extends Model
{
    use UsesTenantConnection, SoftDeletes;
    protected $with=['rentalable'];
    protected $guarded = [];
    public function order()
    {
        return $this->belongsTo(Order::class);
    }

    public function rentalable()
    {
        return $this->morphTo()->withTrashed();
    }

    public function scopeExtraFee($query) {
        return $query->where('rentalable_type', OrderExtraFee::class);
    }

    public function scopeAssets($query) {
        return $query->where('rentalable_type', Asset::class);
    }

    public function scopeAssetStocks($query) {
        return $query->where('rentalable_type', AssetStock::class);
    }
}
